<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="home">
        <a class="site-navbar__brand-lg" href="javascript:;">Personal Blog</a>
        <a class="site-navbar__brand-mini" href="javascript:;">DB</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="fold"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal" @select="rightSelectHandle">
<!--        <el-submenu index="1">
          <template slot="title">实验室
            <el-badge value="new"></el-badge>
          </template>
          <el-menu-item index="2-1"><a href="#" target="_blank">前端</a></el-menu-item>
          <el-menu-item index="2-2"><a href="#" target="_blank">后台</a></el-menu-item>
          <el-menu-item index="2-3"><a href="#" target="_blank">代码生成器</a></el-menu-item>
        </el-submenu> -->
        <el-menu-item class="site-navbar__full-screen" index="2">
          <i class="el-icon-full-screen"></i>
        </el-menu-item>
        <el-menu-item class="site-navbar__theme" index="3">
          <theme-picker :size="size"></theme-picker>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="4">
          <el-dropdown :show-timeout="0" placement="bottom" @command="commandHandle">
            <span class="el-dropdown-link">
              <img :src="headUrl" :alt="nickName" />
              {{ nickName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="root" command="personalHome">个人主页</el-dropdown-item>
              <el-dropdown-item command="updatePassword">修改密码</el-dropdown-item>
              <el-dropdown-item command="styleDrawer">布局</el-dropdown-item>
              <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <main-navbar-update-password ref="updatePassowrd" :size="size"></main-navbar-update-password>
    <!-- 布局设置 -->
    <theme ref="styleDrawer" :size="size"></theme>
  </nav>
</template>

<script>
import { localStorageSet } from '@/utils/storage-util'
import MainNavbarUpdatePassword from '@/views/modules/main/main-navbar-update-password'
import ThemePicker from '@/components/themepicker'
import Theme from '@/views/common/theme'
export default {
  components: { MainNavbarUpdatePassword, ThemePicker, Theme },
  data() {
    return {}
  },
  computed: {
    size: {
      get() { return this.$store.getters.controlSize }
    },
    navbarLayoutType: {
      get() {
        return this.$store.getters.navbarLayoutType
      }
    },
    sidebarFold: {
      get() {
        return this.$store.getters.sidebarFold
      },
      set(val) {
        this.$store.commit('common/SET_SIDEBARFOLD', val)
      }
    },
    mainTabs: {
      get() {
        return this.$store.getters.mainTabs
      },
      set(val) {
        this.$store.commit('common/SET_MAINTABS', val)
      }
    },
    nickName: {
      get() {
        return this.$store.getters.nickName
      }
    },
    headUrl: {
      get() {
        let headUrl = this.$store.getters.headUrl
        let sex = this.$store.getters.sex
        if (!headUrl) {
          headUrl = require('@/assets/img/avatar_' + sex + '.png')
        }
        return headUrl
      }
    },
    root: {
      get() {
        return this.$store.getters.root
      }
    }
  },
  methods: {
    // 首页
    home() {
      this.$router.push({ name: 'home' })
      this.$store.commit('common/SET_MENUCLEAR', true)
    },
    commandHandle(command) {
      if (command === 'personalHome') {
        window.open('https://www.beansprout.top', '_blank')
      } else if (command === 'updatePassword') {
        this.$refs.updatePassowrd.dialogOptions.show = true
        this.$refs.updatePassowrd.init()
      } else if (command === 'styleDrawer') {
        this.$refs.styleDrawer.visible = true
      } else if (command === 'logout') {
        this.logoutHandle()
      }
    },
    // 退出
    logoutHandle() {
      this.$confirm(`确定退出登录吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('user/logout').then(res => {
          this.$router.push({ name: 'login' })
          localStorageSet('AUTOLOGIN', false)
        })
      })
    },
    rightSelectHandle(key, keyPath) {
      switch (parseInt(key)) {
        case 2:
          // 全屏
          this.handleFullScreen()
          break
      }
    },
    checkFull() {
      let isFull = document.webkitIsFullScreen
      if (isFull === undefined) {
        isFull = false
      }
      return isFull
    },
    handleFullScreen() {
      let element = document.documentElement
      if (this.checkFull()) {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen()
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen()
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen()
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen()
        }
      }
      this.fullscreen = !this.fullscreen
    }
  }
}
</script>

<style lang="scss">
.site-navbar {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: 1030;
  height: 50px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);

  &--inverse {
    .site-navbar__body {
      background-color: transparent;
    }
    .el-menu {
      background-color: transparent;
      > .el-menu-item,
      > .el-submenu .el-submenu__title {
        color: #fff;
        &:focus,
        &:hover {
          color: #fff;
        }
      }
      .el-menu-item i,
      .el-submenu__title i,
      .el-dropdown {
        color: #fff;
        display: inline-flex;
      }
    }
  }

  &__header {
    position: relative;
    float: left;
    width: 230px;
    height: 50px;
    overflow: hidden;
  }
  &__brand {
    display: table-cell;
    vertical-align: middle;
    width: 230px;
    height: 50px;
    margin: 0;
    line-height: 50px;
    font-size: 20px;
    text-align: center;
    text-transform: uppercase;
    white-space: nowrap;
    color: #fff;

    &-lg,
    &-mini {
      margin: 0 5px;
      color: #fff;
      &:focus,
      &:hover {
        color: #fff;
        text-decoration: none;
      }
    }
    &-mini {
      display: none;
    }
  }
  &__switch {
    font-size: 18px;
    border-bottom: none !important;
    >.icon-svg__fold {
      width: 30px;
      height: 30px;
    }
  }
  &__full-screen {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
    >.el-icon-full-screen {
      font-size: 30px !important;
    }
  }
  &__theme {
  }
  &__avatar {
    * {
      vertical-align: inherit;
    }
    .el-dropdown-link {
      > img {
        width: 36px;
        height: auto;
        margin-right: 5px;
        border-radius: 100%;
        vertical-align: middle;
      }
    }
  }
  &__body {
    position: relative;
    margin-left: 230px;
    padding-right: 15px;
    background-color: #fff;
  }
  &__menu {
    float: left;
    background-color: transparent;
    border-bottom: none;

    &--right {
      float: right;
    }
    a:focus,
    a:hover {
      text-decoration: none;
    }
    .el-menu-item,
    .el-submenu .el-submenu__title {
      height: 50px !important;
      line-height: 50px !important;
      border-bottom: none !important;
    }
    .el-submenu > .el-menu {
      top: 55px;
    }
    .el-badge {
      display: inline;
      z-index: 2;
      &__content {
        line-height: 16px;
      }
    }
  }
}
</style>
